package cn.samler.project.guestbook.service.impl;

import cn.samler.project.guestbook.entity.Message;
import cn.samler.project.guestbook.entity.User;
import cn.samler.project.guestbook.mapper.MessageMapper;
import cn.samler.project.guestbook.model.common.Page;
import cn.samler.project.guestbook.model.common.Response;
import cn.samler.project.guestbook.model.request.MessageCreateRequest;
import cn.samler.project.guestbook.model.request.MessageModifyRequest;
import cn.samler.project.guestbook.model.request.MessageReplyRequest;
import cn.samler.project.guestbook.model.response.MessageDetail;
import cn.samler.project.guestbook.model.response.PageData;
import cn.samler.project.guestbook.service.MessageService;
import cn.samler.project.guestbook.util.ContextHolderUtil;
import cn.samler.project.guestbook.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
    private final MessageMapper messageMapper;

    public MessageServiceImpl(MessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    @Override
    public Response list(Page page) {
        List<MessageDetail> list = messageMapper.listByPage(PageUtil.getStartRow(page), PageUtil.getEndRow(page));
        int count = messageMapper.count();

        PageData pageData = new PageData();
        pageData.setCount(count);
        pageData.setList(list);
        return Response.builder().ret(Response.Ret.SUCCESS).msg("获取成功").data(pageData).build();
    }

    @Override
    public Response create(MessageCreateRequest request) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        Message message = new Message();
        message.setUserId(user.getId());
        message.setTitle(request.getTitle());
        message.setContent(request.getContent());
        message.setCreateDate(new Date());

        if (messageMapper.insert(message) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("留言成功").build();
        }

        log.error("Failed to create message: {}", message);
        return Response.builder().ret(Response.Ret.FAILURE).msg("留言失败").build();
    }

    @Override
    public Response modify(MessageModifyRequest request) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        Message message = messageMapper.findById(request.getId());
        if (!Objects.equals(user.getId(), message.getUserId())) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("这不是你的留言").build();
        }

        Message update = new Message();
        update.setId(request.getId());
        message.setContent(request.getContent());
        message.setModifyDate(new Date());
        if (messageMapper.update(message) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("修改成功").build();
        }

        log.error("Failed to modify message: {}", message);
        return Response.builder().ret(Response.Ret.FAILURE).msg("修改失败").build();
    }

    @Override
    public Response reply(MessageReplyRequest request) {
        Message message = messageMapper.findById(request.getId());
        if (message.getReplyDate() != null) {
            return Response.builder().ret(Response.Ret.FAILURE).msg("这条留言已经回复过").build();
        }

        Message reply = new Message();
        reply.setId(request.getId());
        reply.setReplyContent(request.getContent());
        reply.setReplyDate(new Date());
        if (messageMapper.update(reply) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("回复成功").build();
        }

        log.error("Failed to reply message: {}", message);
        return Response.builder().ret(Response.Ret.FAILURE).msg("回复失败").build();
    }

    @Override
    public Response delete(Integer id) {
        if (messageMapper.delete(id) > 0) {
            return Response.builder().ret(Response.Ret.SUCCESS).msg("删除成功").build();
        }

        log.error("Failed to delete message: {}", id);
        return Response.builder().ret(Response.Ret.FAILURE).msg("删除失败").build();
    }
}
